package com.itheima.tlias.Controller;

import com.itheima.tlias.Service.DeptService;
import com.itheima.tlias.Service.impl.DeptServiceImpl;
import com.itheima.tlias.anno.Log;
import com.itheima.tlias.pojo.Dept;
import com.itheima.tlias.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Scope("prototype")
@Lazy
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    public  DeptController(){
        System.out.println("DeptController constructor.....");
    }


    @GetMapping
    public Result list() {
        log.info("查询全部部门。");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    @Log
    @DeleteMapping("{id}")
    //删除部门
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据id删除部门",id);
        deptService.delete(id);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增科室");
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("根据id查询科室信息");
        Dept dept=deptService.selectById(id);
        return Result.success(dept);
    }

    @Log
    @PutMapping()
    public Result update(@RequestBody Dept dept){
        log.info("根据id修改科室信息");
        deptService.update(dept);
        return Result.success();
    }


}
